package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.BBS_ReplyDao;
import com.hhit.collaborative_learning.dao.impl.BBS_ReplyDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;

/**
 * Servlet implementation class SetReplySupPointServlt
 */
@WebServlet("/community/SetReplySupPointServlt")
public class SetReplySupPointServlt extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SetReplySupPointServlt() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = request.getParameter("reply_id");
		String tyle = request.getParameter("tyle");
		String style="",isSuccess="false";
		Connection con = DBAccess.getInstance().getConn();
		BBS_ReplyDao rd=new BBS_ReplyDaoImpl();
		JSONObject jobj=new JSONObject();
		int re=0;
		switch (tyle) {
		case "0":
			re=rd.updateSupAndAgaPoint(con, 1, 0, " ID ="+id);
			style="赞成";
			break;
		case "1":
			re=rd.updateSupAndAgaPoint(con, 0, 1, " ID ="+id);
			style="反对";
			break;
		default:
			System.out.println("非法类型 "+tyle);
			style="非法类型";
			break;
		}
		ResultSet reRd=rd.query(con, 0, 0, " br.ID="+id, null, null);
		if(re!=0){
			isSuccess="true";
		}
		try {
			JSONObject rdjson=JsonUtil.formatRsToJSONObject(reRd);
			jobj.put("tyle", style);
			jobj.put("isSuccess", isSuccess);
			jobj.put("reply", rdjson);
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBAccess.getInstance().close(con, null, null);
		}
	}

}
